//
//  NewFeatureCollectionViewController.swift
//  WeiBoOfSwift
//
//  Created by aven wu on 25/04/2018.
//  Copyright © 2018 GML. All rights reserved.
//

import UIKit

private let reuseIdentifier = "NewFeatureCell"
private let pageCount = 4
class NewFeatureCollectionViewController: UICollectionViewController {

    private var layout:NewFeatureLayout = NewFeatureLayout()
    init() {
        super.init(collectionViewLayout: layout)
        
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView?.register(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.showsVerticalScrollIndicator = false
        collectionView?.isPagingEnabled = true
        collectionView?.bounces = false
        
    }
    //MARK: collectionView delegate
    //MARK:返回cell个数
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pageCount
    }
    //MARK:返回cell
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! NewFeatureCell
        cell.backgroundColor = UIColor.randomColor
        cell.imageIndex = indexPath.item
        return cell
    }
    // Mark:完全显示cell时调用
    override func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        // 拿到当前cell现实的索引
        let path = collectionView.indexPathsForVisibleItems.last!
        if path.item == pageCount-1 {
            // 拿到当前索引的cell
            let cell = collectionView.cellForItem(at: path) as! NewFeatureCell
            cell.startButtonAnimation()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: Swift中  文件可以定义多个类
// 自定义cell样式
private class NewFeatureCell:UICollectionViewCell{
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 初始化UI
        setUpUI()
        
    }
    var imageIndex:Int? {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex! + 1)")
        }
    }
   func startButtonAnimation(){
        if imageIndex == (pageCount-1) {
            startButton.isHidden = false
            startButton.isUserInteractionEnabled = false
            startButton.transform = CGAffineTransform.init(scaleX: 0.0, y: 0.0)
            UIView.animate(withDuration: 3, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: UIViewAnimationOptions.curveLinear, animations: {
                self.startButton.transform = CGAffineTransform.identity
            }, completion: { (_) in
                self.startButton.isUserInteractionEnabled = true
            })
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI(){
        // 添加子控件到contentView
        contentView.addSubview(iconView)
        iconView.snp.makeConstraints { (maker) -> Void in
            maker.center.equalTo(self.contentView)
            maker.size.equalTo(self.contentView.snp.size)
        }
        contentView.addSubview(startButton)
        startButton.snp.makeConstraints { (make) ->Void in
            make.centerX.lessThanOrEqualToSuperview()
            make.bottom.equalTo(self.contentView).offset(-20-SAFE_SCREEN_BOTTOM)
        }
    }
    
    //MARK:lazy
    private lazy var iconView = UIImageView()
    private lazy var startButton:UIButton = {
       let  btn = UIButton()
        btn.setBackgroundImage(UIImage(named:"new_feature_button"), for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named:"new_feature_button_highlighted"), for: UIControlState.highlighted)
        btn.isHidden = true
        btn.addTarget(self, action: #selector(starButtonClick), for: UIControlEvents.touchUpInside)
        return btn
    }()
    
   @objc func starButtonClick() -> Void {
        
    }
    
}
// 自定义layout布局
class NewFeatureLayout: UICollectionViewFlowLayout {
    // 什么时候调用？ 1有多少cell --->2调用布局  ---->3调用cell
    override func prepare() {
       print("-------------")
        itemSize = UIScreen.main.bounds.size
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.horizontal
    }
    
}








